package com.test.animation;

import android.app.Activity;
import android.graphics.drawable.AnimationDrawable;
import android.os.Bundle;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.Button;
import android.widget.ImageView;
import android.widget.TextView;

import com.test.R;

public class Animation1 extends Activity {
	
	private TextView textView;
	private Button button;
	private ImageView imageView;
	
	@Override
	protected void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);
		setContentView(R.layout.animation);
		findView();
	}
	
	private void findView(){
		textView = (TextView) findViewById(R.id.textView);
		button = (Button) findViewById(R.id.button);
		imageView = (ImageView) findViewById(R.id.imageView);
		button.setOnClickListener(new OnClickListener() {
			
			@Override
			public void onClick(View v) {
				animate();
			}
		});
	}

	protected void animate() {
		imageView.setVisibility(View.VISIBLE);
		imageView.setBackgroundResource(R.drawable.frame_animation);
		AnimationDrawable frameAnimation = (AnimationDrawable) imageView.getBackground();
		if(frameAnimation.isRunning()){
			frameAnimation.stop();
			textView.setText("2D Animation is stopping");
			button.setText("Start Animation");
		}else{
			frameAnimation.stop();
			frameAnimation.start();
			textView.setText("2D Animation is running");
			button.setText("Stop Animation");
		}
	}
	
	@Override
	protected void onDestroy() {
		super.onDestroy();
		this.finish();
	}
}